<?php
/**
 * Monitoring Grid Component
 * This component display the list of monitoring that a patient has been taken
 * 
 * @author Xavier VILAIN
 * @package \Nawa\MedicalStudy\MedicalCenter\Patient
 * @license
 */
use Nawa\MedicalStudy\Followup;
use Fox\Behaviors;
use Fox\Html\Button;
use Fox\Generator;
use Fox\Grid;
$gridName = 'gMonitorings_'.$params['patientid'];
// Source
$grid = new Grid($gridName,
				Generator::url(
					'medicalstudies',
					'patients',
					'monitorings',
					$params)
);

// Field Catalogue - Part
$grid->fieldcat(array(
			'visit_date',
			'monitoring_followup',
			'monitoring_date',
			array('monitoring_author','author'),
			'corrections_asked',
			array('monitoring_comments','comments'),
			array('patient_validated','validated'),
			'iedit',
			'idele'
			));
// Followup Type
$typeLabels = array();
$ms = Xview::instance()->component('medicalstudies','object',$params);
foreach($ms->followup() as $followup) {
	$typeLabels[$followup->itemid] = $followup->label();
}
$grid->fieldcat()
		->field('visit_date')->width(15);
$grid->fieldcat()
		->field('monitoring_followup')
		->setGenerator(Generator::useFunction(function($data) use($typeLabels) {
			if(!empty($typeLabels[$data['itemid']])) {
				return $typeLabels[$data['itemid']];
			}
		}));
if(!empty($params['itemid'])) {
	$grid->fieldcat()
		->field('monitoring_followup')
		->noOutput();
	$gridName .='_'.$params['itemid'];
	$grid->fieldcat()
		->field('visit_date')
		->noOutput();
}		
$grid->fieldcat()
		->field('corrections_asked')
			->width(10);
$grid->fieldcat()
		->field('validated')
			->width(10);
$grid->fieldcat()
		->field('comments')
			->align('left')
			->width(70);
$grid->fieldcat()
	->field('author')
	->width(20)
	->align('center')
	->headerAlign('center')
	->setGenerator(Generator::useFunction(function($data){
		return \Nawa\Customer::fullName($data['author']);
}));
// Edition
$params['gridname'] = 'MC_'.$gridName;
$crudParams = $params;
$crudParams['monitoringid'] = Generator::useElement('monitoringid');
// Update a monitoring
$grid->fieldcat()->field('iedit')
	->setGenerator(Generator::useValue('?'))
	->renderer()->behaviors(
	Behaviors::action(
		Generator::url('medicalstudies','patients','monitoring',
						$crudParams)
					,
		true,
		null,
		null,
		true
));
// Remove a monitoring
$grid->fieldcat()->field('idele')
	->setGenerator(Generator::useValue('?'))
	->renderer()->behaviors(
	Behaviors::action(
		Generator::url('medicalstudies','patients','monitoring_delete',
						$crudParams)
					,
		true,
		null,
		null,
		true
));

// Grid Actions
$grid->footer()
		->add(new Button('adnew',
				Generator::display('addPartners',
					Generator::url('medicalstudies','patients','monitoring',
							$params),'MC_'.$gridName),
							Msg(Nawa\Messages::BUNDLE,Nawa\Messages::MONITORING_ADD))
				);
return $grid;